package org.javaboy.consumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

/**
 * @author zhaoxiang
 * @version 1.0
 * @Date 2020/3/18 16:24
 */
@RestController
public class UserHelloController {

    @Autowired
    DiscoveryClient discoveryClient;

    /**
     * 传统JavaEE方法远程调用provider服务
     *
     * @return
     */
    @GetMapping("/hello1")
    public String hello1() {
        HttpURLConnection con = null;
        try {
            URL url = new URL("http://localhost:1113/hello");
            con = (HttpURLConnection)url.openConnection();
            if (con.getResponseCode() == 200){
                BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String s = br.readLine();
                br.close();
                return s;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }
        return "error";
    }

    /**
     * 使用 DiscoveryClient 获取服务实例对象
     *
     * @return
     */
    @GetMapping("/hello2")
    public String hello2() {
        // serviceId-需要调用服务的名称
        List<ServiceInstance> list = discoveryClient.getInstances("provider");
        ServiceInstance instance = list.get(0);
        String host = instance.getHost();
        int port = instance.getPort();
        StringBuilder sb = new StringBuilder();
        sb.append("http://").
                append(host).
                append(":").
                append(port).
                append("/hello");

        HttpURLConnection con = null;
        try {
            URL url = new URL(sb.toString());
            con = (HttpURLConnection)url.openConnection();
            if (con.getResponseCode() == 200){
                BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String s = br.readLine();
                br.close();
                return s;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }
        return "error";
    }

    /**
     * 手动实现负载均衡
     *
     * @return
     */
    int count = 0;
    @GetMapping("/hello3")
    public String hello3() {
        // serviceId-需要调用服务的名称
        List<ServiceInstance> list = discoveryClient.getInstances("provider");
        ServiceInstance instance = list.get((count++)%list.size());
        String host = instance.getHost();
        int port = instance.getPort();
        StringBuilder sb = new StringBuilder();
        sb.append("http://").
                append(host).
                append(":").
                append(port).
                append("/hello");

        HttpURLConnection con = null;
        try {
            URL url = new URL(sb.toString());
            con = (HttpURLConnection)url.openConnection();
            if (con.getResponseCode() == 200){
                BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String s = br.readLine();
                br.close();
                return s;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }
        return "error";
    }


    @Autowired
    @Qualifier("restTemplateOne")
    RestTemplate restTemplateOne;
    /**
     * 使用 RestTemplate 远程调用服务
     *
     * @return
     */
    @GetMapping("/hello4")
    public String hello4() {
        // serviceId-需要调用服务的名称
        List<ServiceInstance> list = discoveryClient.getInstances("provider");
        ServiceInstance instance = list.get(0);
        String host = instance.getHost();
        int port = instance.getPort();
        StringBuilder sb = new StringBuilder();
        sb.append("http://").
                append(host).
                append(":").
                append(port).
                append("/hello");
        String str = restTemplateOne.getForObject(sb.toString(), String.class);
        return str;
    }


    @Autowired
    @Qualifier("restTemplate")
    private RestTemplate restTemplate;
    /**
     * Ribbon实现负载均衡
     *
     * @return
     */
    @GetMapping("/hello5")
    public String hello5() {
        String str = restTemplate.getForObject("http://provider/hello", String.class);
        return str;
    }
}
